package servlet;

import bean.Repertory;
import bean.User;
import bean.laundry;
import dao.impl.RepertoryDaoImpl;
import dao.impl.laundryDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author guoxiuwei
 * @Date 2020/8/25 21:09
 * @Title
 */
@WebServlet("/saledServlet")
public class saledServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //防止中文乱码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        String method=request.getParameter("method");

        if (method.equals("saled")){

            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String dateNowStr = sdf.format(date);//当前时间


            laundry l = new laundry();
            int mid = Integer.valueOf(request.getParameter("mid"));
            System.out.println(mid);
            l.setMerchId(mid);
            int count = Integer.valueOf(request.getParameter("count"));
            System.out.println(count);
            Repertory R=new Repertory();
            R.setMerchId(mid);
            R.setMerchSum(count);
            int s=new RepertoryDaoImpl().MerchSumUpdate(R);
            if(s==1){
                l.setSalessum(count);
                double price = new laundryDaoImpl().getprice(mid);
                l.setShouldmoney(price*count);
                //从session中获取操作者信息
                HttpSession session = request.getSession();
                User u = (User)session.getAttribute("us");
                l.setUid(u.getUid());
                l.setRealitymoney(0);
                int f = 0;
                f = new laundryDaoImpl().laundryAdd(l,dateNowStr);
                if(f == 1){
                    response.sendRedirect("saledServlet?method=showLaundryList");
//                response.getWriter().println("支付成功");
                }else {
                    response.getWriter().println("支付失败");
                }
            }else {
                response.getWriter().println("支付失败,库存不够");
            }


        }else if (method.equals("showLaundryList")){
            List<laundry> laundries = new laundryDaoImpl().showLanudry();
            System.out.println(laundries);
            request.setAttribute("lists",laundries);
            //这里要跳转到一个流水的页面
            request.getRequestDispatcher("/liushuidan.jsp").forward(request,response);
        }else if(method.equals("LaundrySearch")){
            int laundryId=Integer.parseInt(request.getParameter("laundryId"));
            System.out.println(laundryId);
            List<laundry> l=new laundryDaoImpl().LaundrySearch(laundryId);
            request.setAttribute("lists",l);
            //这里要跳转到一个流水的页面
            request.getRequestDispatcher("/liushuidan.jsp").forward(request,response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
